using System;
using Sce.PlayStation.Core;

namespace Nyanimals
{
    public class CStatDisplay : CMovingObject
    {
        CTextRender drawText;
        CMovingObject owningObject;
        
        public CTextRender DrawText
        {
            get { return drawText;}
        }
        public CMovingObject OwningObject
        {
            get { return owningObject;}
            set { owningObject = value;}
        }
        
        public CStatDisplay(int width, int height)
        {
            owningObject = null;
            drawText = new CTextRender(width, height);
        }
        
        public override ObjectTypes Type
        {
            get { return ObjectTypes.Moving_Object;}
        }
        
        public override void Input()
        {
        }
        
        public override void Update(float deltaTime)
        {                   
            // only do this if the context is active
            if(context.Active)
            {
                Matrix4 billboard = Matrix4.Translation(owningObject.Position);
                Vector3 camPos = CCamera.Instance.LocalMatrix.AxisW;
                camPos.Y = 0.0f;
                
                CMath.LookAt(ref billboard, camPos, Vector3.UnitY);
                billboard.AxisW = Vector3.Zero;
                context.WorldMatrix = Matrix4.Translation(owningObject.Position) * billboard * Matrix4.Scale(10.0f, 10.0f, 10.0f);     
                
                drawText.DrawTexts();
            }
        }
        
        public override void Init ()
        {
            int meshIndex = CMeshManager.Instance.LoadMesh("unit_plain_z.SRM");
            int contextIndex = CContextManager.Instance.AddStaticContext(meshIndex, Matrix4.Scale(10.0f, 10.0f, 10.0f));
            
            context = (CStaticContext)CContextManager.Instance.GetContext(contextIndex);
            context.debugTexOverride = drawText.RenderTexture;
            context.Active = false;
        }
        
        public override void Shutdown ()
        {
            CContextManager.Instance.RemoveContext(context);
        }
    }
}

